/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "register/tuning_bank_key_registry.h"
#include "common/ge_common/debug/ge_log.h"

namespace tuningtiling {
OpBankKeyFuncInfo::OpBankKeyFuncInfo(const ge::AscendString &optype) : optype_(optype) {}

OpBankKeyFuncInfoV2::OpBankKeyFuncInfoV2(const ge::AscendString &optypeV2) : optypeV2_(optypeV2) {}

// 待v2版本上库后删除
void OpBankKeyFuncInfo::SetOpConvertFunc(const OpBankKeyConvertFun &convert_func) {
  convert_func_ = convert_func;
}

void OpBankKeyFuncInfo::SetOpParseFunc(const OpBankParseFun &parse_func) {
  parse_func_ = parse_func;
}

void OpBankKeyFuncInfo::SetOpLoadFunc(const OpBankLoadFun &load_func) {
  load_func_ = load_func;
}


// 上库后删除上面的函数定义
void OpBankKeyFuncInfoV2::SetOpConvertFuncV2(const OpBankKeyConvertFunV2 &convert_funcV2) {
  convert_funcV2_ = convert_funcV2;
}

void OpBankKeyFuncInfoV2::SetOpParseFuncV2(const OpBankParseFunV2 &parse_funcV2) {
  parse_funcV2_ = parse_funcV2;
}

void OpBankKeyFuncInfoV2::SetOpLoadFuncV2(const OpBankLoadFunV2 &load_funcV2) {
  load_funcV2_ = load_funcV2;
}

// 待v2版本上库后删除
const OpBankKeyConvertFun& OpBankKeyFuncInfo::OpBankKeyFuncInfo::GetBankKeyConvertFunc() const {
  return convert_func_;
}

const OpBankParseFun& OpBankKeyFuncInfo::GetBankKeyParseFunc() const {
  return parse_func_;
}

const OpBankLoadFun& OpBankKeyFuncInfo::GetBankKeyLoadFunc() const {
  return load_func_;
}

// 上库后删除上面的函数定义
const OpBankKeyConvertFunV2& OpBankKeyFuncInfoV2::OpBankKeyFuncInfoV2::GetBankKeyConvertFuncV2() const {
  return convert_funcV2_;
}

const OpBankParseFunV2& OpBankKeyFuncInfoV2::GetBankKeyParseFuncV2() const {
  return parse_funcV2_;
}

const OpBankLoadFunV2& OpBankKeyFuncInfoV2::GetBankKeyLoadFuncV2() const {
  return load_funcV2_;
}

// 待v2版本上库后删除
std::unordered_map<ge::AscendString, OpBankKeyFuncInfo> &OpBankKeyFuncRegistry::RegisteredOpFuncInfo() {
  static std::unordered_map<ge::AscendString, OpBankKeyFuncInfo> op_func_map;
  return op_func_map;
}

// 上库后删除上面的函数定义
std::unordered_map<ge::AscendString, OpBankKeyFuncInfoV2> &OpBankKeyFuncRegistryV2::RegisteredOpFuncInfoV2() {
  static std::unordered_map<ge::AscendString, OpBankKeyFuncInfoV2> op_func_mapV2;
  return op_func_mapV2;
}

extern "C" void _ZN12tuningtiling21OpBankKeyFuncRegistryC1ERKN2ge12AscendStringERKSt8functionIFbRKSt10shared_ptrIvEmRN15ascend_nlohmann10basic_jsonISt3mapSt6vectorSsblmdSaNSA_14adl_serializerESD_IhSaIhEEEEEERKS5_IFbRS7_RmRKSH_EE() {}

// 待废弃接口
OpBankKeyFuncRegistry::OpBankKeyFuncRegistry(const ge::AscendString &optype, const OpBankKeyConvertFun &convert_func) {
  auto &op_func_map = RegisteredOpFuncInfo();
  const auto iter = op_func_map.find(optype);
  if (iter == op_func_map.cend()) {
    OpBankKeyFuncInfo op_func_info(optype);
    op_func_info.SetOpConvertFunc(convert_func);
    (void)op_func_map.emplace(optype, op_func_info);
  } else {
    iter->second.SetOpConvertFunc(convert_func);
  }
}

OpBankKeyFuncRegistry::OpBankKeyFuncRegistry(const ge::AscendString &optype,
                                             const OpBankParseFun &parse_func, const OpBankLoadFun &load_func) {
  auto &op_func_map = RegisteredOpFuncInfo();
  const auto iter = op_func_map.find(optype);
  if (iter == op_func_map.cend()) {
    OpBankKeyFuncInfo op_func_info(optype);
    op_func_info.SetOpParseFunc(parse_func);
    op_func_info.SetOpLoadFunc(load_func);
    (void)op_func_map.emplace(optype, op_func_info);
  } else {
    iter->second.SetOpParseFunc(parse_func);
    iter->second.SetOpLoadFunc(load_func);
  }
}

// 新增接口
OpBankKeyFuncRegistryV2::OpBankKeyFuncRegistryV2(const ge::AscendString &optype, const OpBankKeyConvertFunV2 &convert_funcV2) {
  auto &op_func_mapV2 = RegisteredOpFuncInfoV2();
  const auto iter = op_func_mapV2.find(optype);
  if (iter == op_func_mapV2.cend()) {
    OpBankKeyFuncInfoV2 op_func_info(optype);
    op_func_info.SetOpConvertFuncV2(convert_funcV2);
    (void)op_func_mapV2.emplace(optype, op_func_info);
  } else {
    iter->second.SetOpConvertFuncV2(convert_funcV2);
  }
}

OpBankKeyFuncRegistryV2::OpBankKeyFuncRegistryV2(const ge::AscendString &optype, const OpBankParseFunV2 &parse_funcV2,
                                                 const OpBankLoadFunV2 &load_funcV2) {
  auto &op_func_map = RegisteredOpFuncInfoV2();
  const auto iter = op_func_map.find(optype);
  if (iter == op_func_map.cend()) {
    OpBankKeyFuncInfoV2 op_func_info(optype);
    op_func_info.SetOpParseFuncV2(parse_funcV2);
    op_func_info.SetOpLoadFuncV2(load_funcV2);
    (void)op_func_map.emplace(optype, op_func_info);
  } else {
    iter->second.SetOpParseFuncV2(parse_funcV2);
    iter->second.SetOpLoadFuncV2(load_funcV2);
  }
}
}  // namespace tuningtiling